package com.situ.company.score.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.situ.company.project.model.ProjectModel;
import com.situ.company.score.model.ScoreModel;
import com.situ.company.score.service.ScoreService;
import com.situ.company.util.FmtRequest;

@WebServlet("/score")
public class ScoreServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	ScoreService service = new ScoreService();
	
	 private List<Object> objs = new ArrayList<>();
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action = req.getParameter("action");
		String result="";
		
		switch(action) {
		case "add":
			result = add(req);
			break;
		case "upd":
			result = upd(req);
			break;
		case "list":
			result = list(req);
			break;
		case "get":
			result = get(req);
			break;
		case "del":
			result = del(req);
			break;
		default:
			break;	
		}
		
		FmtRequest.out(resp,result);
	}

	private String del(HttpServletRequest req) {
		// TODO Auto-generated method stub
		String empcode =req.getParameter("empcode");
		String procode =req.getParameter("procode");
		ScoreModel model = new ScoreModel(empcode,procode);
		return service.delete(model);
	}

	private String get(HttpServletRequest req) {
		// TODO Auto-generated method stub
		String empcode =req.getParameter("empcode");
		objs.add(empcode);
		String procode =req.getParameter("procode");
		objs.add(procode);
		ScoreModel model = new ScoreModel(empcode,procode);
		ScoreModel mod = service.selectModel(model);
		return new JSONObject(mod).toString();
	}

	private String list(HttpServletRequest req) {
		// TODO Auto-generated method stub
		String empcode =req.getParameter("empcode");
		String procode =req.getParameter("procode");
		String pageIndex =req.getParameter("pageIndex");
		String pageLimit =req.getParameter("pageLimit");
		ScoreModel model = new ScoreModel(empcode,procode);
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		Map<String,Object> map = new HashMap<>();
		map.put("data", service.selectList(model));
		map.put("count", service.selectCount(model));
		return new JSONObject(map).toString();
		
	}

	private String upd(HttpServletRequest req) {
		// TODO Auto-generated method stub
		String empcode =req.getParameter("empcode");
		String procode =req.getParameter("procode");
		String score =req.getParameter("score");
		ScoreModel model = new ScoreModel(empcode,procode,score);
		String result = service.update(model,objs);
		objs.clear();
		System.out.println(objs.toString());
		return result;
	}

	private String add(HttpServletRequest req) {
		// TODO Auto-generated method stub
		String empcode =req.getParameter("empcode");
		String procode =req.getParameter("procode");
		String score =req.getParameter("score");
		ScoreModel model = new ScoreModel(empcode,procode,score);
		return service.insert(model);
	}
}
